from django.conf import settings

from EE.models import Decision

from django.contrib.auth.models import User

def createNew(reference_state, reference_balance, reference_debt):
  decision = Decision(game=reference_state, balance=reference_balance, debt=reference_debt)
  decision.save()
  return decision
  
def handle_made_turn(reference_state, reference_balance, reference_debt):
  try:
    decision = getDecision(reference_state)
    decision.balance = reference_balance
    decision.debt = reference_debt
  except Decision.DoesNotExist:
    decision = createNew(reference_state, reference_balance, reference_debt)
  decision.save()
  return decision
  
def handle_end_round(reference_state):
  decision = getDecision(reference_state)
  state = reference_state
  state.balance = decision.balance
  state.debt = decision.debt
  state.save()
  
def getDecision(reference_state):
  decision = Decision.objects.filter(game=reference_state).get()
  return decision
  
def clear(decision):
  decision.balance = 0
  decision.debt = 0
  decision.save()